package com.baitiaojun.mybatisplus.core.utils;

import com.baitiaojun.common.domain.model.page.PageParam;
import com.baitiaojun.common.domain.model.page.PageSorted;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * mybatisplus工具类扩展
 */
public class MybatisUtils {

    public static <T> Page<T> buildPage(PageParam pageParam, Collection<PageSorted> pageSorted) {
        Page<T> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        return page.addOrder(pageSorted.stream()
                .map(sorted -> PageSorted.ASC.equals(sorted.getSortedType()) ?
                        OrderItem.asc(sorted.getField()) : OrderItem.desc(sorted.getField()))
                .collect(Collectors.toList()));
    }
}
